<?php

namespace app\api\controller;

use app\common\validate\SmsValidate;
use think\Cache;
use think\Request;

class Sms
{

    const NOT_PARAM = '缺少参数';

    protected $code = '';

    /**
     * 获取短信验证码
     */
    public function sendSms(Request $request){
        $param = $request->param();
        if (empty($param['phone'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'phone 不能为空');
        }

        (new SmsValidate())->goCheck();
        /*if($param['client_key'] != config('sms.client_key')){
            return show(config('status.ERROR_STATUS'), '客户端密钥错误', '客户端密钥错误');
        }*/
        $smsObj = new \app\lib\Sms();
        $result = $smsObj->send($param['phone']);

        if($result['msg'] == 'ok'){
            //$this->code = $result['code'];
            //cookie('sms_code', $result['code']);
            $data = [
                'code' => $result['code'],
                'phone'=> $param['phone']
            ];
            Cache::set('smsCode',$data,60);
            return show(config('status.SUCCESS_STATUS'), '短信发送成功', $result);
        }else{
            return show(config('status.ERROR_STATUS'), '短信发送失败', '短信发送失败');
        }
    }

    /**
     * 验证短信验证码
     */
    public function checkSmsCode(Request $request){
        $param = $request->param();
        if (empty($param['phone'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'phone 不能为空');
        }
        if (empty($param['code'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'code 不能为空');
        }
        $data = Cache::get('smsCode');
        //验证手机是否是同一个
        if ($param['phone'] != $data['phone']){
            return show(config('status.ERROR_STATUS'),'手机号,验证码不匹配!','手机号,验证码不匹配!');
        }
        if($param['code'] == $data['code']){
            return show(config('status.SUCCESS_STATUS'), '验证码正确', '验证码正确');
        }else{
            return show(config('status.ERROR_STATUS'), '验证码错误', '验证码错误');
        }
    }

    /**
     * 验证app短信验证码
     */
    public function checkApiSmsCode(Request $request){
        $param = $request->param();
        if (empty($param['code'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'code 不能为空');
        }
        $data = Cache::get('smsCode');
        //验证手机是否是同一个
        if ($param['phone'] != $data['phone']){
            return show(config('status.ERROR_STATUS'),'手机号,验证码不匹配!','手机号,验证码不匹配!');
        }
        if($param['code'] == $data['code']){
            return show(config('status.SUCCESS_STATUS'), '验证码正确', $data['secrect']);
        }else{
            return show(config('status.ERROR_STATUS'), '验证码错误', '验证码错误');
        }
    }

    //app密钥管理验证码
    public function ApisendSms(Request $request){
        (new SmsValidate())->goCheck();
        $param = $request->param();
        $secrect = db('apptoken')->where('id',1)->field('phone,secrect')->find();
        if($secrect){
            if(trim($param['phone']) != $secrect['phone']){
                return show(config('status.ERROR_STATUS'), '手机号不匹配', []);
            }
            $smsObj = new \app\lib\Sms();
            $result = $smsObj->send($param['phone'],6);

            if($result['msg'] == 'ok'){
                //$this->code = $result['code'];
                //cookie('sms_code', $result['code']);
                $data = [
                    'code' => $result['code'],
                    'phone'=> $param['phone'],
                    'secrect'=> $secrect['secrect']
                ];
                Cache::set('smsCode',$data,60);
                return show(config('status.SUCCESS_STATUS'), '短信发送成功', $result);
            }else{
                return show(config('status.ERROR_STATUS'), '短信发送失败', []);
            }
        }else{
            return show(config('status.ERROR_STATUS'), '系统错误', []);
        }


    }

}